﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Compression.Controls
{
    public class CustomNumericUpDown : NumericUpDown
    {
        decimal[] arr = new decimal[5] { 32, 64, 128, 526, 1024 };

        public override void DownButton()
        {
            decimal newValue = this.Value;
            newValue /= 2;
            if (newValue < 32) newValue = 32;
            
            this.Value = newValue;
        }

        public override void UpButton()
        {
            decimal newValue = this.Value;
            newValue *= 2;
            if (newValue > 1024) newValue = 1024;

            this.Value = newValue;
        }

        protected override void OnValueChanged(EventArgs e)
        {
            decimal curValue = this.Value;
            int num = 0;
            decimal min = decimal.MaxValue;
            for (int i = 0; i < arr.Length; i++)
            {
                if (Math.Abs(arr[i] - curValue) < min)
                {
                    min = Math.Abs(arr[i] - curValue);
                    num = i;
                }
            }
            this.Value = arr[num];
        }
    }
}
